package com.softserveinc.moviedbapp.map;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.softserveinc.moviedbapp.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MapViewer extends MapActivity {

	private MapView mMapView;
	private MapController mMapController;

	private LocationManager mLocationManager;
	private Location mLocation;

	private static final String TAG = MapViewer.class.getSimpleName();

	@Override
	protected void onCreate( final Bundle icicle ) {

		super.onCreate( icicle );
		setContentView( R.layout.map_layout );

		mMapView = (MapView) findViewById( R.id.themap );
		mMapView.setBuiltInZoomControls( true );

		mMapController = mMapView.getController();
		mLocationManager = (LocationManager) getSystemService( LOCATION_SERVICE );

		if ( mLocationManager == null ) {
			Toast.makeText( this, "Location Manager Not Available", Toast.LENGTH_SHORT ).show();
			return;
		}
		mLocation = mLocationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );

		if ( mLocation == null ) {
			Log.d( TAG, "no gps provider getting network provider" );
			mLocation = mLocationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
		}

		/* show user's current location */
		if ( mLocation != null ) {
			double lastLatitude = mLocation.getLatitude();
			double lastLongitude = mLocation.getLongitude();
			Toast.makeText( this, "Location coordinates are  " + lastLatitude + " : " + lastLongitude,
					Toast.LENGTH_LONG ).show();
			GeoPoint lastPoint = new GeoPoint( (int) ( lastLatitude * 1E6 ), (int) ( lastLongitude * 1E6 ) );
			mMapController.animateTo( lastPoint );
			mMapController.setZoom( 12 );

		}

		/* add a location marker */
		Drawable balloon = getResources().getDrawable( R.drawable.balloon_on_stick );

		/* create new itemized overlay and set my balloon to that overlay */
		BalloonItemizedOverlay itemizedOverlay = new BalloonItemizedOverlay( balloon, mMapView );

		/* make pop up to show up higher */
		itemizedOverlay.setBalloonBottomOffset( 60 );
		addToItemizedOverlay( itemizedOverlay );

		mMapView.getOverlays().add( itemizedOverlay );
		mMapView.invalidate();

		LocationListener locationListener = new LocationListener() {

			@Override
			public void onStatusChanged( final String provider, final int status, final Bundle extras ) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderEnabled( final String provider ) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderDisabled( final String provider ) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onLocationChanged( final Location location ) {
				int lastLatitude = (int) ( location.getLatitude() * 1e6 );
				int lastLongitude = (int) ( location.getLongitude() * 1e6 );

				Log.d( TAG, "Location now is   " + lastLatitude + "   " + lastLongitude );

				mMapController.animateTo( new GeoPoint( lastLatitude, lastLongitude ) );
				mMapView.invalidate();
			}
		};
		mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 1000, locationListener );
	}

	/* build geopoints and overlay items and add them to itemizedOverlay */
	private void addToItemizedOverlay( final BalloonItemizedOverlay itemizedOverlay ) {
		Map<Integer, ArrayList<String>> cinemas = CinemaInfoProvider.readCinemaInfo( this );

		Iterator<Integer> iterator = cinemas.keySet().iterator();

		while ( iterator.hasNext() ) {
			Integer key = iterator.next();
			List<String> cinemaInfo = cinemas.get( key );

			int currentLatitude = (int) ( Double.parseDouble( cinemaInfo.get( 0 ) ) * 1e6 );
			int currentLongitude = (int) ( Double.parseDouble( cinemaInfo.get( 1 ) ) * 1e6 );

			GeoPoint currentPoint = new GeoPoint( currentLatitude, currentLongitude );
			Log.d( TAG, cinemaInfo.get( 2 ) + "   " + cinemaInfo.get( 3 ) );
			OverlayItem item = new OverlayItem( currentPoint, cinemaInfo.get( 2 ), cinemaInfo.get( 3 ) );

			itemizedOverlay.addOverlay( item );
		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
